package com.cloudkinto.controller.api;

import com.cloudkinto.base.BaseController;
import com.cloudkinto.bo.order.outbound.OutboundCancelAddBo;
import com.cloudkinto.bo.order.outbound.TransferOrderOutboundSaveBo;
import com.cloudkinto.bo.stock.ProductStockBySkuQueryBo;
import com.cloudkinto.bo.stock.api.ProductStockBySkuQueryApiBo;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.dto.stock.query.SkuStockPageDto;
import com.cloudkinto.enumType.order.outbound.TransferOutboundStatusEnum;
import com.cloudkinto.service.order.outbound.OutboundCancelService;
import com.cloudkinto.service.order.outbound.OutboundService;
import com.cloudkinto.service.order.outbound.vo.api.TransferOrderOutboundApiSaveBo;
import com.cloudkinto.service.stock.ProductStockService;
import com.cloudkinto.vo.common.CommonBatchOperationVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;

@RestController
@RequiredArgsConstructor
@Api(tags = "company1002Api开放接口")
@RequestMapping("/api/admin/third/company1002")
public class ThirdCompany1002ApiController  extends BaseController {
    private final ProductStockService productStockService;
    private final OutboundService outboundService;
    private final OutboundCancelService cancelService;

    @UserSessionRequired
    @PostMapping("/listPageBySku")
    @ApiOperation(value = "按产品查询库存", notes = "按产品查询库存")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = SkuStockPageDto.class))
    public PageResult<SkuStockPageDto> listPageBySku(@RequestBody @Validated ProductStockBySkuQueryApiBo req) {
        ProductStockBySkuQueryBo queryBo = new ProductStockBySkuQueryBo();
        BeanUtils.copyProperties(req, queryBo);
        queryBo.setCompanyIdList(Collections.singletonList(getCompanyId()));
        queryBo.setSource("oms");
        queryBo.setTenantId(getTenantId());
        queryBo.setUserId(getCurrentUserId());
        return productStockService.productListPageBySku(queryBo);
    }


    @UserSessionRequired
    @PostMapping("/transferSave")
    @ApiOperation(value = "中转出库单提交", notes = "中转出库单提交")
    public SingleResult<String> transferSave(@RequestBody @Validated TransferOrderOutboundApiSaveBo bo) {
        TransferOrderOutboundSaveBo saveBo = new TransferOrderOutboundSaveBo();
        BeanUtils.copyProperties(bo, saveBo);
        saveBo.setStatus(TransferOutboundStatusEnum.one.getValue());
        saveBo.setLogisticsChannelId(-1L);//自动选取最便宜的物流渠道
        outboundService.transferSave(saveBo, getCurrentUserId(), getUserName(), getTenantId(), getCompanyId());
        return SingleResult.success("新增成功");
    }


    @UserSessionRequired
    @PostMapping("/cancel/batchAdd")
    @ApiOperation(value = "出库单截单新增", notes = "出库单截单新增")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = CommonBatchOperationVo.class))
    public SingleResult<CommonBatchOperationVo> add(@RequestBody @Validated OutboundCancelAddBo req) {
        return SingleResult.success("新增成功", cancelService.add(req, getCurrentUserId(), getUserName()));
    }
}
